<template>
  <div v-if="systemMsg.length > 0" class="bs-sysMsg">
    <SvgIcon icon-class="count-title" />
    <div class="msg_content">
      <el-carousel
        height="20px"
        direction="vertical"
        :interval="2000"
        indicator-position="none"
        :autoplay="true"
      >
        <el-carousel-item v-for="item in systemMsg" :key="item">
          <span class="item">{{ item }}</span>
        </el-carousel-item>
      </el-carousel>
    </div>
  </div>
</template>
<script>
import { memberStatistics } from '@/api/zzxt/party-group'

export default {
  name: 'News',
  data: () => ({
    systemMsg: [],
    inner_data: [
      { id: 1, count: 0 },
      { id: 3, count: 0 },
      { id: 4, count: 0 },
      { id: 5, count: 0 },
      { id: 6, count: 0 },
      { id: 7, count: 0 }
    ],
    statisticsData: []
  }),
  computed: {
    typeInParty() {
      return this.$store.state.party.typeInParty
    },
    currentCompany() {
      return this.$store.state.user.globalCompany
    }
  },
  watch: {
    currentCompany: {
      handler(val) {
        if (!val) return
        if (!this.typeInParty) this.waitMsgInit()
        this.msgContentInit()
      },
      immediate: true
    }
  },
  mounted() {},
  methods: {
    waitMsgInit() {
      if (!this.typeInParty) {
        return setTimeout(() => {
          this.waitMsgInit()
        }, 200)
      }
      this.msgContentInit()
    },
    msgContentInit() {
      this.systemMsg = []
      this.msgItemInit(this.currentCompany)
      this.msgItemInit(this.currentCompany.slice(0, 2))
    },
    msgItemInit(companyCode) {
      memberStatistics({ company: companyCode }).then(data => {
        const ar = this.inner_data
        const re = data.model
        this.statisticsData = ar.map(item => {
          const id = this.typeInParty[item.id].value
          item.count = re[id] || 0
          return item
        })
        let msgItem = companyCode === this.currentCompany ? '本支部：' : '全单位：'
        this.statisticsData.forEach(item => {
          msgItem += this.typeInParty[item.id].alias
          msgItem += ':'
          msgItem += item.count
          msgItem += '人；'
        })
        this.systemMsg.push(msgItem)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../style';
</style>
